package cn.jiguang.jgadpubadapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;


import androidx.annotation.Nullable;

import cn.jiguang.jgssp.ad.data.ADJgNativeAdInfo;
import cn.jiguang.jgssp.ad.data.ADJgNativeExpressAdInfo;
import xyz.adscope.amps.ad.nativead.adapter.AMPSNativeAdExpressListener;
import xyz.adscope.amps.ad.nativead.adapter.AMPSNativeAdapter;
import xyz.adscope.amps.ad.nativead.inter.AMPSNativeAdExpressInfo;


public class CSJNativeAd implements AMPSNativeAdExpressInfo {
    public ADJgNativeAdInfo ttNativeExpressAd;
    private Context context;
    private final AMPSNativeAdapter adapter;
    private final int ecpm;

    public CSJNativeAd(ADJgNativeAdInfo ttNativeExpressAd, Context context,
                       AMPSNativeAdapter adapter, int ecpm) {
        this.ttNativeExpressAd = ttNativeExpressAd;
        this.context = context;
        this.adapter = adapter;
        this.ecpm = ecpm;
    }


    public ADJgNativeAdInfo getADJgInfo() {
        return ttNativeExpressAd;
    }

    FrameLayout frameLayout = null;

    @Override
    public View getNativeExpressAdView() {
        if (ttNativeExpressAd != null) {
            if (ttNativeExpressAd.isNativeExpress()) {
                if (frameLayout == null) {
                    frameLayout = new FrameLayout(context);
                }
                ((ADJgNativeExpressAdInfo) ttNativeExpressAd).getNativeExpressAdView(frameLayout);
            }
        }
        return null;
    }

    private static final String TAG = "JgPuAdNativeAdapter";

    @Override
    public void render() {
        Log.e(TAG, ": native ad render");
        if (adapter != null) {
            adapter.onRender();
        }
        if (ttNativeExpressAd != null) {
            if (ttNativeExpressAd instanceof ADJgNativeExpressAdInfo) {
                if (frameLayout == null) {
                    frameLayout = new FrameLayout(context);
                }
                View view = ((ADJgNativeExpressAdInfo) ttNativeExpressAd).getNativeExpressAdView(frameLayout);
                ((ADJgNativeExpressAdInfo) ttNativeExpressAd).render(frameLayout);
                ampsNativeAdExpressListener.onRenderSuccess(view, view.getWidth(), view.getHeight());
                return;
            }
        }
        ampsNativeAdExpressListener.onRenderFail(getNativeExpressAdView(), "", 0);
    }

    @Override
    public void destroy() {
        try {
            if (ttNativeExpressAd != null) {
                if (ttNativeExpressAd instanceof ADJgNativeExpressAdInfo) {
                    ttNativeExpressAd.release();
                }

            }
            ttNativeExpressAd = null;
            context = null;
            frameLayout = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private AMPSNativeAdExpressListener ampsNativeAdExpressListener;

    @Nullable
    public AMPSNativeAdExpressListener getAmpsListener() {
        return ampsNativeAdExpressListener;
    }

    @Override
    public void setAMPSNativeAdExpressListener(AMPSNativeAdExpressListener ampsNativeAdExpressListener) {
        Log.e(TAG, ":setListenter1 " );
        if (ttNativeExpressAd != null) {
            if (ampsNativeAdExpressListener != null) {
                this.ampsNativeAdExpressListener = ampsNativeAdExpressListener;
            }
        }
    }


    @Override
    public int getECPM() {
        return ecpm;
    }

}
